package com.laxia.controller.admin;


import com.laxia.aspect.LoginValidate;
import com.laxia.dao.AdminUserDAO;
import com.laxia.domain.entity.AdminUser;
import com.laxia.enums.TokenTypeEnum;
import com.laxia.exception.ensure.Ensure;
import com.laxia.model.Result;
import com.laxia.model.dtos.LoginDTO;
import com.laxia.model.vos.LoginVO;
import com.laxia.util.ImageVerificationCodeUtil;
import com.laxia.util.RedisUtil;
import com.laxia.util.SessionUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;


/**
 * 后台登陆
 *
 * @author weishuai
 * @since 2020/3/2
 */
@RestController
@AllArgsConstructor
@Slf4j
public class AdminLoginController {


    RedisUtil redisUtil;

    AdminUserDAO adminUserDAO;


    /**
     * 获取验证码图片
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/admin/getVCode")
    public void getVerifiCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ImageVerificationCodeUtil ivc = new ImageVerificationCodeUtil();
        //获取验证码
        BufferedImage image = ivc.getImage();
        //获取随机码
        String text = ivc.getText();
        //设置到session
        SessionUtil.addCurrentVcode(text);
        request.getSession().setAttribute(SessionUtil.IMG_CODE, text);

        //设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");
        //设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        //将验证码图片响应给客户端
        ImageVerificationCodeUtil.output(image, response.getOutputStream());
    }


    /**
     * 图片验证码登陆
     */
    @PostMapping("/admin/login")
    @ResponseBody
    public Result<LoginVO> login(@Validated @RequestBody LoginDTO.AdminLoginParam param) {
        LoginVO vo = new LoginVO();
        String code = SessionUtil.getCurrentVcode();
        log.info("验证码2:code:{}", code);
        Ensure.that(param.getVcode().equalsIgnoreCase(code)).isTrue("验证码错误");

        //校验登陆
        AdminUser adminUser = adminUserDAO.getByUsername(param.getUsername());
        Ensure.that(adminUser).isNotNull("用户名不存在");
        Ensure.that(adminUser.getState() == 0).isTrue("账号已被冻结!");

        Ensure.that(adminUser.getPassword().equals(param.getPassword())).isTrue("用户名和密码不匹配");

        //生成token给前端
        vo.setIsAdmin(adminUser.getIsAdmin());
        vo.setToken(SessionUtil.generateLoginToken(TokenTypeEnum.MANAGE, adminUser.getId()));
        vo.setRealName(adminUser.getRealName());
        return Result.success(vo);
    }

    /**
     * 退出登陆
     */
    @PostMapping("/admin/loginOut")
    @ResponseBody
    @LoginValidate
    public Result loginOut() {
        SessionUtil.removeSessionUser();
        return Result.success();
    }


}
